package net.obviam.starassault.screens;

import net.obviam.logic.Simulation;
import net.obviam.starassault.controller.WorldController;
import net.obviam.starassault.model.World;
import net.obviam.starassault.view.WorldRenderer;

import com.badlogic.gdx.Application.ApplicationType;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input.Keys;
import com.badlogic.gdx.InputProcessor;
import com.badlogic.gdx.Screen;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.math.Rectangle;

public class GameScreen implements Screen, InputProcessor {

	public WorldRenderer renderer;
	public Simulation sim;
	private int width, height;
	public boolean gameOver = false;

	@Override
	public void show() {
		sim = new Simulation(this);
		renderer = new WorldRenderer(this, true);
		Gdx.input.setInputProcessor(this);
	}

	@Override
	public void render(float delta) {
		Gdx.gl.glClearColor(0.1f, 0.1f, 0.1f, 1);
		Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);

		sim.update();
		renderer.camRender();
	}

	@Override
	public void resize(int width, int height) {
		renderer.setSize(width, height);
		this.width = width;
		this.height = height;
	}

	@Override
	public void hide() {
		Gdx.input.setInputProcessor(null);
	}

	@Override
	public void pause() {
		// TODO Auto-generated method stub
	}

	@Override
	public void resume() {
		// TODO Auto-generated method stub
	}

	@Override
	public void dispose() {
		Gdx.input.setInputProcessor(null);
	}

	// * InputProcessor methods ***************************//

	@Override
	public boolean keyDown(int keycode) {
		if (keycode == Keys.LEFT)
			sim.wc.leftPressed();
		if (keycode == Keys.RIGHT)
			sim.wc.firePressed();
		if (keycode == Keys.SPACE)
			sim.wc.jumpPressed();
		if (keycode == Keys.X)
			sim.wc.firePressed();
		return true;
	}

	@Override
	public boolean keyUp(int keycode) {
		if (keycode == Keys.LEFT)
			sim.wc.leftReleased();
		if (keycode == Keys.RIGHT)
			sim.wc.fireReleased();
		if (keycode == Keys.SPACE)
			sim.wc.jumpReleased();
		if (keycode == Keys.X)
			sim.wc.fireReleased();
		return true;
	}

	@Override
	public boolean keyTyped(char character) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean touchDown(int x, int y, int pointer, int button) {	
		if (!Gdx.app.getType().equals(ApplicationType.Android))
			return false;
		if (x < width / 2 && y > height / 2) {
			sim.wc.jumpPressed();
		}
		if (x > width / 2 && y > height / 2) {
			sim.wc.firePressed();
		}
		return true;
	}

	@Override
	public boolean touchUp(int x, int y, int pointer, int button) {
		if (!Gdx.app.getType().equals(ApplicationType.Android))
			return false;
		if (x < width / 2 && y > height / 2) {
			sim.wc.jumpReleased();
		}
		if (x > width / 2 && y > height / 2) {
			sim.wc.fireReleased();
		}

		return true;
	}

	@Override
	public boolean touchDragged(int x, int y, int pointer) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean touchMoved(int x, int y) {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public boolean scrolled(int amount) {
		// TODO Auto-generated method stub
		return false;
	}


}
